﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace iLangl.LanguageWire.DAL.iLanglDAL.XMLStorage
{
	public class BaseStorageDAL
	{
		public dtoXMLStorage Storage
		{
			get
			{
				return XMLStorageManager.Instance.Storage;
			}
		}

		public void AcceptChanges()
		{
			XMLStorageManager.Instance.AcceptChanges();
		}

		public bool IsRequiredToSync(TimeSpan syncTimeSpam, TimeSpan currentTimeSpan)
		{
			return syncTimeSpam < currentTimeSpan;
		}

		public void UpdateDeliveryTime(eEntityType type, String objectId, TimeSpan time)
		{
			if (time == TimeSpan.MinValue)
				return;

			dtoXMLStorage.SyncDataRow sdr = Storage.SyncData.FindByObjectTypeObjectId(type.ToString(), objectId);

			bool isNew = (sdr == null);

			if (isNew)
			{
				sdr = Storage.SyncData.NewSyncDataRow();
			}

			sdr.ObjectId = objectId;
			sdr.ObjectType = type.ToString();
			sdr.SyncTimeStamp = time;

			if (isNew)
			{
				Storage.SyncData.AddSyncDataRow(sdr);
			}

			AcceptChanges();
		}

		public TimeSpan GetSyncSpan(eEntityType type, String objectId)
		{
			dtoXMLStorage.SyncDataRow sdr = Storage.SyncData.FindByObjectTypeObjectId(type.ToString(), objectId);

			if (sdr != null)
				return sdr.SyncTimeStamp;

			return new TimeSpan(DateTime.MinValue.Ticks);
		}

		public TimeSpan CurrentTimeSpan
		{
			get
			{
				return new TimeSpan(DateTime.Now.Ticks);
			}
		}

	}
}
